#ifndef GE_IMAGE_HPP
#define GE_IMAGE_HPP

#include "ge_color.hpp"
#include <iostream>
#include <vector>

namespace ge {
  class image {
    public:
      image();
      
      /// Get width of the image
      int width() const;
      /// Get height of the image
      int height() const;
      
      /// Takes a pixel coordinate and returns its color
      color pixel_at(unsigned int const x, unsigned int const y) const;
      
      /// Returns a pointer to the buffer
      float * buffer();
      
      /** Reads an image from a stream. 
        Most of the time the stream will be an ifstream, i.e., a file.
        @throw std::runtime_error if the file format is not supported.
        @note currently, only the PNG file format is supported.
      */
      friend std::istream & operator>>(std::istream & in, image & img);
    private:
      int _width, _height;
      std::vector<float> _pixels;
  };
}

namespace ge {

  inline int image::width() const {
    return _width;
  }
   
  inline int image::height() const {
    return _height;
  }
      
  inline ge::color image::pixel_at(unsigned int const x, unsigned int const y) const {
    return color(_pixels[4 * x + 0 + 4 * y * _width],
                 _pixels[4 * x + 1 + 4 * y * _width],
                 _pixels[4 * x + 2 + 4 * y * _width],
                 _pixels[4 * x + 3 + 4 * y * _width]);
  }
  
  inline float * image::buffer() {
    return &_pixels[0];
  }
}

#endif
